﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Reflection;

namespace ESN.Languages
{
    public class LangString
    {
        public static string Load(string strname, string classname)
        {
            var langname = "en";
            var Request = HttpContext.Current.Request;
            if (Request.Cookies["userpref"] != null && Request.Cookies["userpref"]["lang"] != null)
            {
                langname = Request.Cookies["userpref"]["lang"];
            }

            String s = GetString(strname, classname, langname);

            if (s == null)
            {
                //I can sure that Englash string allways full!
                s = GetString(strname, classname, "en");
            }
            return s;
        }
        private static string GetString(string strname, string classname, string lang)
        {
            Type LangType = Type.GetType("ESN.Languages." + lang + "." + classname + ",ESN");

            String s = (String)LangType.InvokeMember(
                    "Locale",
                    BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
                    null,
                    null,
                    new Object[] { strname });
            return s;
        }
    }
}